import { login, logout, getInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import {getStoreID} from "../../api/login";
import {deleteCookie, setCookie} from "../../utils/support";

const user = {
  state: {
    token: getToken(),
    name: '',
    avatar: '',
    roles: [],
    storeList:[]
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_STOREID: (state, storeList) => {
      state.storeList = storeList;
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      // const username = userInfo.username.trim()
      let token='123';
      setToken(token);
      commit('SET_TOKEN', token);
      // return new Promise((resolve, reject) => {
      //   login(username, userInfo.password).then(response => {
      //
      //     console.log(response)
      //     const data = response.data
      //
      //     const tokenStr = data
      //     console.log(tokenStr)
      //     setToken(tokenStr)
      //     commit('SET_TOKEN', tokenStr)
      //     resolve()
      //   }).catch(error => {
      //     reject(error)
      //   })
      // })
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      const role =
      commit('SET_ROLES', ["超级管理员"]);
          commit('SET_NAME', "admin");
          commit('SET_AVATAR', "http://macro-oss.oss-cn-shenzhen.aliyuncs.com/mall/images/20180607/timg.jpg")
      // return new Promise((resolve, reject) => {
      //   getInfo().then(response => {
      //     const data = response.data
      //     if (data.roles && data.roles.length > 0) { // 验证返回的roles是否是一个非空数组
      //       commit('SET_ROLES', data.roles)
      //     } else {
      //       reject('getInfo: roles must be a non-null array !')
      //     }
      //     commit('SET_NAME', data.username)
      //     commit('SET_AVATAR', data.icon)
      //     resolve(response)
      //   }).catch(error => {
      //     reject(error)
      //   })
      // })
    },
    //获取当前登录状态下的商家id
    getStoreID({commit}){
      return new Promise((resolve, reject) =>{
        getStoreID().then(res=>{
          const data=res.data;
           commit('SET_STOREID', data);
           setCookie('storeId',data[0].storeId);
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 登出
    LogOut({ commit, state }) {
      // return new Promise((resolve, reject) => {
      //   logout(state.token).then(() => {
      //     commit('SET_TOKEN', '');
      //     commit('SET_ROLES', []);
      //     deleteCookie('storeId')
      //     removeToken();
      //     resolve()
      //   }).catch(error => {
      //     reject(error)
      //   })
      // })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        deleteCookie('storeId')
        removeToken()
        resolve()
      })
    }
  }
}

export default user
